import {
	createRouter,
	createWebHistory
} from 'vue-router'

const routes = [
	{
		path: '/home',
		name: 'home',
		component: () => import('../views/home/index.vue'),
		children: [
			{
				path: '/home',
				redirect: "/home/welcome"
			},
			{
				path: 'welcome',
				name: 'welcome',
				component: () => import("../views/welcome.vue")
			},
			{
				path: 'user',
				name: 'user',
				component: () => import("../views/user/user.vue")
			},
			{
				path: 'shop',
				name: 'shop',
				component: () => import("../views/shop/shop.vue")
			},
			{
				path: 'merchant',
				name: 'merchant',
				component: () => import("../views/merchant/merchant.vue")
			},
			{
				path: '/order1',
				name: 'order1',
				component: () => import("../views/order/order1.vue")
			},
			{
				path: '/order2',
				name: 'order2',
				component: () => import("../views/order/order2.vue")
			},
			{
				path: '/order3',
				name: 'order3',
				component: () => import("../views/order/order3.vue")
			},
			{
				path: '/order4',
				name: 'order4',
				component: () => import("../views/order/order4.vue")
			},
			{
				path: '/order5',
				name: 'order5',
				component: () => import("../views/order/order5.vue")
			},
			{
				path: '/order6',
				name: 'order6',
				component: () => import("../views/order/order6.vue")
			},

		]
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/login/index.vue')
	},
	{
		path: "/chatlogin",
		component: () => import("../views/customerChat/Login.vue"),
	},
	{
		path: '/customer',
		name: "customer",
		component: () => import('../views/customerChat/Home.vue'),
		children: [
			{
				path: 'conversations',
				component: () => import("../views/customerChat/Conversations.vue"),
				children: [{
					path: 'chat/:id',
					component: () => import("../views/customerChat/Chat.vue")
				}]
			},
			{
				path: 'customers',
				component: () => import("../views/customerChat/Customers.vue")
			}
		]
	},
	{
		path: '/',
		redirect: '/login'
	},
]

const router = createRouter({
	history: createWebHistory(),
	routes
})
const whiteList = ['/login', '/'] // 不重定向白名单
router.beforeEach((to, from, next) => {
	sessionStorage.setItem('token', 'Bearer 123456kljhkaslffmkdjasklsdkfmjbsanjkslvpbkmjnasdfdbvksdjkj')
	const isLogin = sessionStorage.getItem('token')
	if (isLogin) {
		if (to.path === '/login') {
			next()
		} else {
			next()
		}
	} else {
		if (whiteList.indexOf(to.path) > -1) {
			next()
		} else {
			next('/login')
		}
	}
})


export default router